package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.qc.QcRule;
import com.ysd.lis.mapper.qc.QcRuleMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.qc.QcRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class QcRuleServiceImpl extends ServiceImpl<QcRuleMapper, QcRule> implements QcRuleService {

    @Autowired
    QcRuleMapper qcRuleMapper;
    @Override
    public Result findRulePageList(CommonPageDto pageParam) {
        Page<QcRule> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<QcRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QcRule::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(QcRule::getName, pageParam.getSeachValue())
                    .or().like(QcRule::getCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(QcRule::getSeq);
        Page<QcRule> rulePage = qcRuleMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", rulePage);
    }
    @Override
    public Result findQcRuleList(CommonPageDto pageParam) {
        LambdaQueryWrapper<QcRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QcRule::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(QcRule::getName, pageParam.getSeachValue())
                    .or().like(QcRule::getCode, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(QcRule::getSeq);
        return Result.succ(1, "查询成功", qcRuleMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public Result addRule(QcRule rule) {
        int insert = qcRuleMapper.insert(rule);
        if(insert>0){
            return Result.succ(1,"添加成功",rule);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editRule(QcRule rule) {
        int updateById = qcRuleMapper.updateById(rule);
        if(updateById>0){
            return Result.succ(1,"提交成功",rule);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteRule(QcRule rule) {
        int deleteById = qcRuleMapper.deleteById(rule.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }


}
